package cn.edu.zzuli.admin.controller;

import cn.edu.zzuli.admin.service.OrderService;
import cn.edu.zzuli.admin.service.UserService;
import cn.edu.zzuli.common.result.Result;
import cn.edu.zzuli.common.result.ResultInfo;
import cn.edu.zzuli.model.dto.admin.UserInfoDto;
import cn.edu.zzuli.model.entity.OfferOrder;
import cn.edu.zzuli.model.entity.User;
import cn.edu.zzuli.model.vo.identity.UserQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api("后台用户管理接口")
@RequestMapping("/background/admin/user")
public class ApiUserController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @ApiOperation("根据分页参数以及选择的条件查询列表")
    @GetMapping("list/{page}/{limit}")
    public ResultInfo list(@PathVariable Integer page,
                           @PathVariable Integer limit,
                           UserQueryVo userQueryVo) {
        Page<User> identityPage = userService.findAll(page, limit, userQueryVo);
        return Result.ok(identityPage);
    }

    @ApiOperation("查询用户详情信息")
    @GetMapping("getUserInfo/{id}")
    public ResultInfo getUserInfo(@PathVariable Integer id) {
        UserInfoDto user = userService.getUserInfoById(id);
        return Result.ok(user);
    }

    @ApiOperation("批量删除用户")
    @DeleteMapping("batchRemove")
    public ResultInfo batchRemove(@RequestBody List<Integer> idList) {
        boolean flag = userService.removeByIds(idList);
        return flag ? Result.ok("成功") : Result.fail("删除失败");
    }

    @ApiOperation("删除用户")
    @DeleteMapping("delete/{id}")
    public ResultInfo deleteUser(@PathVariable Integer id) {
        boolean res = userService.removeById(id);
        return res == true ? Result.ok() : Result.fail("删除失败");
    }

    @ApiOperation("修改用户状态")
    @GetMapping("updateUser/{id}/{status}")
    public ResultInfo updateUser(@PathVariable Integer id,
                                 @PathVariable Integer status) {
        User user = userService.getById(id);
        user.setLocked(status);
        boolean res = userService.updateById(user);
        return res == true ? Result.ok() : Result.ok("修改失败");
    }

    @GetMapping("update/{ouid}/{bounty}/{oid}")
    public ResultInfo updateWealth(@PathVariable Integer ouid,
                                   @PathVariable Double bounty,
                                   @PathVariable Integer oid) {
        User user = userService.getById(ouid);
        user.setWealth(user.getWealth() + bounty);
        boolean res = userService.updateById(user);

        OfferOrder of = new OfferOrder();
        of.setOuid(ouid);
        of.setRefund(1);
        of.setOid(oid);
        orderService.updateById(of);

        return res == true ? Result.ok() : Result.fail();
    }


}

